package com.junyidao.game.controller;

import com.junyidao.game.Exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by HP on 2016/8/11.
 */
public class BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public BaseController() {
    }

    @ResponseBody
    @ExceptionHandler
    public Map<String, Object> exp(HttpServletRequest request, Exception ex) {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("＃＃＃＃＃＃＃＃＃＃{}", ex);
        }

        HashMap rs = new HashMap();
        if (ex instanceof BusinessException) {
            rs.put("msg", ex.getMessage());
            rs.put("code", Integer.valueOf(((BusinessException) ex).getCode()));
            return rs;
        } else {
            rs.put("msg", ex.getMessage());
            rs.put("code", Integer.valueOf(1));
            return rs;
        }
    }

    public Map<String, Object> resultSucess(Object obj) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", new Integer(0));
        map.put("msg", "操作成功");
        map.put("data", obj);
        return map;

    }

    public Map<String, Object> resultError(String msg) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", new Integer(1));
        map.put("msg", msg);
        map.put("data", null);
        return map;

    }

}
